package secondkill.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import secondkill.Exception.GlobalException;
import secondkill.mapper.TUserMapper;
import secondkill.pojo.TUser;
import secondkill.service.ITUserService;
import secondkill.utils.CookieUtil;
import secondkill.utils.MD5Util;
import secondkill.utils.UUIDUtil;
import secondkill.vo.LoginVo;
import secondkill.vo.RespBean;
import secondkill.vo.RespBeanEnum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Dzy
 * @since 2022-01-13
 */
@Slf4j
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements ITUserService {

    @Autowired
    private TUserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录
     * @param loginVo 
     * @param request 
     * @param response
     * @return secondkill.vo.RespBean 
     * @create 2022/1/19 13:08
     */
    @Override
    public RespBean doLogin(LoginVo loginVo, HttpServletRequest request, HttpServletResponse response) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //参数校验 在自定义了IsMobile注解后可以省略判断
        /*if (StringUtils.isEmpty(mobile)||StringUtils.isEmpty(password)){
            return RespBean.error(RespBeanEnum.LOGIN_ERROR);
        }
        if(ValidatorUtil.isMobile(mobile)){
            return RespBean.error(RespBeanEnum.MOBILE_ERROR);
        }*/

        TUser user = userMapper.selectById(mobile);
        if(user==null){
            //return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }
        //判断密码是否正确
        if(!MD5Util.formPass_To_DBPass(password,user.getSalt()).equals(user.getPassword())){
            //return RespBean.error(RespBeanEnum.LOGIN_ERROR);
            throw new GlobalException(RespBeanEnum.LOGIN_ERROR);
        }

        //生成cookie
        String ticket = UUIDUtil.uuid();
        //将用户信息存入 session
        //request.getSession().setAttribute(ticket,user);

        //将用户信息存入 redis 中
        redisTemplate.opsForValue().set("tUser:" + ticket, user);
        CookieUtil.setCookie(request,response,"tUserTicket",ticket);

        return RespBean.success(ticket);
    }

    /**
     * 根据cookie获取用户
     * @param userTicket
     * @param request 
     * @param response
     * @return secondkill.pojo.TUser 
     * @create 2022/1/19 13:08
     */
    @Override
    public TUser getUserByCookie(String userTicket, HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isEmpty(userTicket)){
            return null;
        }
        TUser user = (TUser)redisTemplate.opsForValue().get("tUser:" + userTicket);
        if (user != null){
            CookieUtil.setCookie(request,response,"tUserTicket",userTicket);
        }
        return user;
    }

    /**
     * 用户更新密码
     * @param tUserTicket
     * @param password
     * @param request
     * @param response
     * @return secondkill.vo.RespBean
     * @create 2022/1/23 1:22
     */
    @Override
    public RespBean updatePassword(String tUserTicket, String password, HttpServletRequest request, HttpServletResponse response) {

        TUser user = getUserByCookie(tUserTicket, request, response);
        if (user == null) {
            throw new GlobalException(RespBeanEnum.MOBILE_NOT_EXIST);
        }

        user.setPassword(MD5Util.inputPass_To_DBPass(password, user.getSalt()));
        int result = userMapper.updateById(user);
        if (1 == result) {
            redisTemplate.delete("tUser:" + tUserTicket);
            return RespBean.success();
        }
        return RespBean.error(RespBeanEnum.UPDATE_FAIL);
    }
}
